package com.gxust.mapper;

import com.gxust.dao.OrderDao;
import com.gxust.dto.ProductDto;
import com.gxust.pojo.Orders;
import com.gxust.vo.OrderSellerVO;
import com.gxust.vo.ProductVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface OrderMapper {


    @Insert("INSERT INTO `orders` (`UserID`,`AddressID`, `OrderDate`, `OrderStatus`, `TotalAmount`)\n" +
            "VALUES (#{userId},#{addressId}, CURRENT_TIMESTAMP, '未支付',#{totalAmount})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId",keyColumn = "OrderID")
    void addOrders(OrderDao orderDao);

    @Insert("INSERT INTO orderitems (`OrderID`, `ProductID`, `Quantity`, `UnitPrice`, `SellerId`)\n" +
            "SELECT #{OrderId} AS `OrderID`, \n" +
            "       p.ProductID, \n" +
            "       c.Quantity, \n" +
            "       p.ProductPrice, \n" +
            "       p.SellerId\n" +
            "FROM cart c\n" +
            "JOIN product p ON c.ProductID = p.ProductID\n" +
            "WHERE c.UserID = #{userId} AND p.ProductID = #{productId}")
    void addorderitems(Integer OrderId,Integer userId, Integer productId);

    @Select("select * from orders where UserID = #{userId} ORDER BY OrderDate DESC")
    List<Orders> getIdByUserId(Integer userId);

    @Select("SELECT ProductId, Quantity AS ProductNum FROM orderitems WHERE OrderID = #{orderId}; ")
    List<ProductDto> getProductListByOrderId(Integer orderId);

    @Select("select o.OrderID from orderitems os,orders o  where SellerId = #{id}  group by OrderID")
    List<Integer> selectOrderIdBySellerId(Integer id);

    @Select("select * from orders where OrderID = #{integer}")
    Orders getOrderByOrderId(Integer integer);

    @Results({
            @Result(property = "order.orderId", column = "orderId"),
            @Result(property = "order.userId", column = "userId"),
            @Result(property = "order.addressId", column = "addressId"),
            @Result(property = "order.orderDate", column = "orderDate"),
            @Result(property = "order.orderStatus", column = "orderStatus"),
            @Result(property = "order.totalAmount", column = "totalAmount"),
            @Result(property = "address",column = "addressId",
            one = @One(select = "com.gxust.mapper.AddressMapper.getAddressById",fetchType = FetchType.EAGER))
    })
    @Select("select o.* from orderitems os, orders o where SellerId = #{id} " +
            "and os.OrderID=o.OrderID  group by OrderID")
    List<OrderSellerVO> selectOrderBySellerId(Integer id);

    @Results({
            @Result(property = "product",column = "ProductID",
                    one=@One(select = "com.gxust.mapper.ProductMapper.productbyid",fetchType = FetchType.EAGER)
            ),
            @Result(property = "productNum",column ="Quantity"),
            @Result(property = "productTotalPrice",column = "TotalPrice")
    })
    @Select("SELECT p.ProductID,os.Quantity,os.TotalPrice from product p,orderitems os where p.ProductID=os.ProductID and os.OrderID=#{orderId} and os.SellerId=#{id}")
    List<ProductVO> getProductListByOrderIdAndSellerId(Integer orderId, Integer id);
}
